﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sealight.Core.Domain;
using Sealight.API.Framework;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Linq;

namespace Sealight.Data
{
   public class OrderDataRepository:NHibernateRepository<OrderEntity>, IOrderDataRepository
    {

        #region IOrderDataRepository Members

       public OrderProductVariantEntity AddOrderDetail(OrderProductVariantEntity entity)
        {
            try
            {
                UnitOfWork.CurrentSession.SaveOrUpdate(entity);
            }
            catch (ADOException ex)
            {
                //rethrow the exception
                throw;
            }

            return entity;
        }

       public OrderNoteEntity AddOrderNote(OrderNoteEntity entity)
        {
            try
            {
                UnitOfWork.CurrentSession.SaveOrUpdate(entity);
            }
            catch (ADOException ex)
            {
                //rethrow the exception
                throw;
            }

            return entity;
        }

        public OrderProductVariantEntity GetOrderDetail(Guid id)
        {
            OrderProductVariantEntity result = null;
            try
            {
                result = UnitOfWork.CurrentSession.Get<OrderProductVariantEntity>(id);
            }
            catch (ADOException ex)
            {
                //rethrow the exception
                throw;
            }

            return result;
        }

        public OrderNoteEntity GetOrderNote(Guid id)
        {
            OrderNoteEntity result = null;
            try
            {
                result = UnitOfWork.CurrentSession.Get<OrderNoteEntity>(id);
            }
            catch (ADOException ex)
            {
                //rethrow the exception
                throw;
            }

            return result;
        }

        public IList<OrderProductVariantEntity> GetOrderDetails(Guid order_id)
        {
            IList<OrderProductVariantEntity> result = null;
            try
            {
                result = UnitOfWork.CurrentSession.CreateCriteria<OrderProductVariantEntity>()
                    .CreateAlias("Order", "o")
                    .Add(Expression.Eq("o.OrderID", order_id))
                    .List<OrderProductVariantEntity>();
            }
            catch (ADOException ex)
            {
                //rethrow the exception
                throw;
            }

            return result;
        }
        public IList<OrderEntity> SearchOrders(string keyword, DateTime? from, DateTime? to)
        {
            IList<OrderEntity> result = null;
            try
            {
                IQueryable<OrderEntity> data = UnitOfWork.CurrentSession.Query<OrderEntity>();

                if (from.HasValue && to.HasValue)
                {
                    if (!string.IsNullOrEmpty(keyword))
                    {
                        result = data.Where(c => c.Name.Contains(keyword) &&c.CreatedDate >= from.Value && c.CreatedDate<=to.Value ).ToList();
                    }
                    else
                    {
                        result = data.Where(c => c.CreatedDate >= from.Value && c.CreatedDate<=to.Value).ToList();
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(keyword))
                    {
                        result = data.Where(c => c.Name.Contains(keyword)).ToList();
                    }
                }
            }
            catch (ADOException ex)
            {
                //rethrow the exception
                throw;
            }
            return result;
        }
        #endregion
    }
}
